package com.jie.jie.camera;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jaydenxiao.common.commonutils.LogUtils;
import com.jaydenxiao.common.commonutils.ToastUitl;
import com.jaydenxiao.common.v.dialog.CommonDialogTwoBtn;
import com.jie.jie.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class CameraActivity extends AppCompatActivity {

    @Bind(R.id.camera_btn)
    Button cameraBtn;
    @Bind(R.id.my_camera_btn)
    Button myCameraBtn;
    @Bind(R.id.activity_camera)
    LinearLayout activityCamera;
    @Bind(R.id.carema_iv)
    ImageView caremaIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.camera_btn, R.id.my_camera_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.camera_btn:
                CameraActivityPermissionsDispatcher.callCameraWithCheck(this);
                break;
            case R.id.my_camera_btn:
                CameraActivityPermissionsDispatcher.callCamera1WithCheck(this);
                break;
        }
    }

    @NeedsPermission(Manifest.permission.CAMERA)
    void callCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 200);
    }

    @OnShowRationale(Manifest.permission.CAMERA)
    void show(final PermissionRequest request) {
        new CommonDialogTwoBtn.Builder(this)
                .setMessage("需要授予拍照权限")
                .setPositiveButton("好的", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.proceed();
                    }
                })
                .setNegativeButton("算了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.cancel();
                    }
                }).create();
    }

    @OnPermissionDenied(Manifest.permission.CAMERA)
    void Denied() {
        ToastUitl.showShort("拒绝了权限授予");
    }

    @OnNeverAskAgain(Manifest.permission.CAMERA)
    void noAsk() {
        ToastUitl.showShort("不在提示");
    }

    @NeedsPermission(Manifest.permission.CAMERA)
    void callCamera1() {
        if (checkCameraHardware(this)) {
            Intent intent = new Intent(CameraActivity.this, DIYCameraActivity.class);
            startActivity(intent);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.loge(String.valueOf(resultCode)+"             "+resultCode);
        if (requestCode == 200) {
            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");
            caremaIv.setImageBitmap(bitmap);
        }
    }

    private boolean checkCameraHardware(Context context) {
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            return true;
        } else {
            return false;
        }
    }
}
